package cn.szzsi._web;

import cn.szzsi.common.bean.dto.Msg;
import cn.szzsi.common.kit.SessionUtil;
import com.jfinal.core.Controller;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.UserApi;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by agfly on 2016/11/16.
 */
public class WeiServiceController extends Controller {

    /**
     * 生成二维码
     */
    /*public void qrcode() {
        String apiid = getPara("apiid");
        System.out.println("=======" + apiid);

        String scene_id = getPara("scene_id");
        SiteConfig siteConfig = SiteConfig.getByApiid(apiid);
        Member member = Member.dao.findById(scene_id);
        if (member != null) {
            Map<String, String> para = new HashMap<>();
            para.put("url", CustomApi.GET_QRCODE_URL + medium.getStr("qrcode"));
            renderJson(Msg.success(para));
            return;
        }
        String qrurl = "";
        String qr = "";
        try {
            ApiConfigKit.setThreadLocalApiConfig(SessionUtil.getApiConfigForB(this));
            ApiResult apiResult = QrcodeApi.createPermanent(scene_id);
            Integer errcode = apiResult.getInt("errcode");
            if (null != errcode && errcode == 40001) {
                apiResult = QrcodeApi.createPermanent(scene_id);
            }
            System.out.println("創建二维码**************" + apiResult);
            qrurl = apiResult.getStr("url");
            qr = apiResult.getStr("ticket");
        } finally {
            ApiConfigKit.removeThreadLocalApiConfig();
        }

        Map<String, String> map = new HashMap<>();
        map.put("url", CustomApi.GET_QRCODE_URL + qr);
        renderJson(Msg.success(map));
    }*/

    /**
     * 获取用户信息
     */
    public void userInfo() {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            ApiConfigKit.setThreadLocalApiConfig(SessionUtil.getApiConfigForB(this));
            ApiResult apiResult = UserApi.getUserInfo(getPara("openid"));
            Integer errcode = apiResult.getInt("errcode");
            System.out.println("apiResult:"+apiResult);
            if (null != errcode && errcode == 40001) {
                apiResult = UserApi.getUserInfo(getPara("openid"));
            }
            String openid = apiResult.getStr("openid");
            String nickname = apiResult.getStr("nickname");
            Integer sex = apiResult.getInt("sex");
            String city = apiResult.getStr("city");
            String province = apiResult.getStr("province");
            String country = apiResult.getStr("country");
            String headimgurl = apiResult.getStr("headimgurl");
            map.put("openid", openid);
            map.put("nickname", nickname);
            map.put("sex", sex);
            map.put("city", city);
            map.put("province", province);
            map.put("country", country);
            map.put("headimgurl", headimgurl);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ApiConfigKit.removeThreadLocalApiConfig();
        }
        renderJson(Msg.success(map));
    }

    /**
     * 获取微信appid
     */
    /*public void getAppid() {
        String apiid = getPara("apiid");
        if (StringUtils.isBlank(apiid)) {
            renderJson(Msg.fail(1, "apiid不能为空"));
            return;
        }
        BusinessApplicationWeixin baw = BusinessApplicationWeixin.getByApiid(apiid);
        if (baw == null) {
            renderJson(Msg.fail(1, "参数错误"));
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("appid", baw.getStr("apiid"));
        map.put("appsecret", baw.getStr("appsecret"));
        renderJson(Msg.success(map));
    }*/
}
